package com.example.androidmenu;

import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class androidMenu extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_menu);
        TextView tv=new TextView(this);
        tv.setText("Menu btn");
        registerForContextMenu(tv);
        setContentView(tv);
    }
    public boolean onCreateOptionsMenu(Menu menu){
        menu.add(0,1,1,"Save");
        SubMenu sub=menu.addSubMenu("Search");
        sub.setIcon(android.R.drawable.ic_menu_search);
        sub.add(0,1,1,"Local");
        sub.add(0,2,2,"Internet");
        menu.add(0,2,2,"Exit");
        return super.onCreateOptionsMenu(menu);
    }
    public boolean onOptionItemSelected(MenuItem item){
        if(item.getItemId()==2){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
    public boolean onContextItemSelected(MenuItem item){
        super.onContextItemSelected(item);
        switch (item.getItemId()){
            case 1:
            case 2:break;
        }
        return super.onContextItemSelected(item);
    }
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
        menu.add(0,1,1,"New");
        menu.add(0,2,2,"Open");
        super.onCreateContextMenu(menu,v,menuInfo);
    }
}
